package org.boge.sharpdoc.service;

import org.boge.sharpdoc.dao.DocGroupDao;
import org.boge.sharpdoc.bean.DocGroup;
import org.boge.sharpdoc.bean.TreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DocGroupService {
    @Autowired
    private DocGroupDao docGroupDao;

    public List<DocGroup> getAllFirstStepGroup() {
        return docGroupDao.findFirstStep();
    }

    //获取子分类
    public List<DocGroup> getGroupChildren(Integer groupid) {
        return docGroupDao.findChildren(groupid);
    }

    public Boolean changeName(DocGroup docGroup) {
        return docGroupDao.updateName(docGroup) != 0;
    }

    public Integer addNewGroup(DocGroup docGroup) {
        docGroupDao.addItem(docGroup);
        return docGroup.getGroupid();
    }

    public Boolean deleteItem(DocGroup docGroup) {
        return docGroupDao.deleteItem(docGroup) > 0;
    }

    //获取所有子分类，递归
    public List<DocGroup> getAllGroupChildren(DocGroup docGroup) {
        List<DocGroup> res = new ArrayList<>();
        res.add(docGroup);
        for (DocGroup item : docGroup.getChildren()) {
            res.addAll(getAllGroupChildren(item));
        }
        return res;
    }

    public DocGroup findById(Integer groupid) {
        return docGroupDao.findById(groupid);
    }

    public TreeData getTreeData() {
        return docGroupDao.getTreeData();
    }
}